// Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
#include <csignal>
#endif
#include <chrono>
#include <condition_variable>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <thread>

#include <vsomeip/vsomeip.hpp>
#include <vsomeip/internal/logger.hpp>

#include "network/include/Defs.h"
#include "network/include/VsomeipClient.h"


#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
    client_sample *its_sample_ptr(nullptr);
    void handle_signal(int _signal) {
        if (its_sample_ptr != nullptr && (_signal == SIGINT || _signal == SIGTERM))
        {
            its_sample_ptr->stop();
        }
    }
#endif

int main(int argc, char **argv) {
    bool use_tcp = false;

    if(argc == 2)
    {
        std::string input {argv[1]};
        if(input == "5" || input == "100" || input == "101")
        {
            client_sample its_sample(use_tcp, std::atoi(argv[1]));
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
            its_sample_ptr = &its_sample;
            signal(SIGINT, handle_signal);
            signal(SIGTERM, handle_signal);
#endif
            if (its_sample.init()) {
                std::thread t([&](){
                    std::this_thread::sleep_for(std::chrono::seconds(1));
                    its_sample.sendRequest();
                });
                its_sample.start();
                t.join();
                return 0;
            } else {
                return 1;
            }
        }
    }
    std::cout << "Example : VSOMEIP_CONFIGURATION=\"../../config/udp_client.json\" ./VsomeipClient 5" << std::endl;
    return -1;
}
